package com.huonan.jb.tools.esuse.export.apps;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.setting.dialect.Props;
import com.huonan.jb.tools.esuse.config.PropConfig;
import com.huonan.jb.tools.esuse.dto.EsHitsInfo;
import com.huonan.jb.tools.esuse.export.Constants;
import com.huonan.jb.tools.esuse.export.enums.Env;
import com.huonan.jb.tools.esuse.export.enums.FileType;
import com.huonan.jb.tools.esuse.export.es.SearchEs;
import com.huonan.jb.tools.esuse.export.type.ExportFile;
import com.huonan.jb.tools.esuse.export.utils.EsUtils;
import com.huonan.utils.DateHelper;
import com.huonan.utils.ExecutorUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Description 导出数据到文件，所以使用 * 匹配
 * @Author lxp
 * @Date 2023/2/6 下午2:31
 * @Version V1.0
 **/
@Slf4j
public class AppEs {

    static String argFile = Constants.SQL_FILE;
    /*线程数=导数并行度（指定时间切分几分）*/
    //导出数据格式。默认json
    static FileType fileType = FileType.CSV;
    static Env env = Env.PROD;

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        /*线程数=导数并行度（指定时间切分几分）*/
        int THREAD_SIZE = PropConfig.getThreadSize(argFile);
        ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_SIZE);
        // sql 不变
//        for (int i = 1; i <= 1; i++) {
//            Props props = EsUtils.getProps(THREAD_SIZE, i, argFile);
//            threadSearch(props, threadPool);
//        }
        // sql 中查询条件动态变化
        String sorceFilePath = "/Users/lxp/IdeaProjects/Study/MainTest/data/bayou-ec/2023-10-10/";
        List<File> files = FileUtil.loopFiles(sorceFilePath);
        List<String> list = new ArrayList<>();
        for (File file : files) {
            List<String> selectFields = FileUtil.readUtf8Lines(file);
            list.addAll(selectFields);
        }
        Props props = EsUtils.getProps(THREAD_SIZE, 1, argFile);
        for (String field : list) {
            String sql = props.getStr("sql");
            String newSql = String.format(sql, field);
            props.setProperty("sql", newSql);
            CompletableFuture<Void> future = threadSearch(props, threadPool);
            future.get();
        }
        ExecutorUtil.closePool(threadPool);
    }


    private static CompletableFuture<Void> threadSearch(Props props, ExecutorService threadPool) {
        return CompletableFuture.runAsync(() -> {
            DateHelper.MonitorUtil.start();
            int searchCount = 0;
            String scrollId;
            while (true) {
                searchCount++;
                try {
                    EsHitsInfo dataResult = SearchEs.preHandler(env, true, props);
                    if (EsUtils.isContinue(dataResult)) continue;
                    JSONArray hits = dataResult.getHits();
                    if (EsUtils.isBreak(hits)) break;
                    scrollId = dataResult.getScrollId();
                    props.put("scrollId", scrollId);
                    ExportFile.exportHandle(props, dataResult, fileType);
                    log.info("{} 第:{}次,数据总量:{},本次导出数据量:{}", Thread.currentThread().getName(), searchCount,
                            dataResult.getHistTotal(), hits.size());
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error(e.getMessage());
                    return;
                }
            }
        }, threadPool);
    }
}